package com.zlp.api.controller;


import com.zlp.api.test.config.ConnectElasticsearch;
import com.zlp.api.test.config.ElasticsearchTask;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class WeightFunctionQueryTest {



    public static final ElasticsearchTask SEARCH_WITH_ORDER = client -> {
        //1.创建request
        SearchRequest request = new SearchRequest("goods");
        //2.指定查询条件
        SearchSourceBuilder builder = new SearchSourceBuilder();
        BoolQueryBuilder newNumberLte30 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("newNumber").lte(30));
        BoolQueryBuilder newNumberGt30Lte60 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("newNumber").gt(30).lte(60));
        BoolQueryBuilder newNumberGt60Lte90 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("newNumber").gt(60).lte(90));
        BoolQueryBuilder newNumberGt90 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("newNumber").gt(90));

        BoolQueryBuilder browseGt1000 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("browse").gt(1000));
        BoolQueryBuilder browseGt500Lte1000 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("browse").gt(500).lte(100));
        BoolQueryBuilder browseGt100Lte500 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("browse").gt(100).lte(500));
        BoolQueryBuilder browseLt100 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("browse").lt(100));

        List<FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();

        filterFunctionBuilders.add(new FilterFunctionBuilder(newNumberLte30,ScoreFunctionBuilders.weightFactorFunction(1000)));
        filterFunctionBuilders.add(new FilterFunctionBuilder(newNumberGt30Lte60,ScoreFunctionBuilders.weightFactorFunction(900)));
        filterFunctionBuilders.add(new FilterFunctionBuilder(newNumberGt60Lte90,ScoreFunctionBuilders.weightFactorFunction(500)));
        filterFunctionBuilders.add(new FilterFunctionBuilder(newNumberGt90,ScoreFunctionBuilders.weightFactorFunction(100)));
        filterFunctionBuilders.add(new FilterFunctionBuilder(browseGt1000,ScoreFunctionBuilders.weightFactorFunction(800)));
        filterFunctionBuilders.add(new FilterFunctionBuilder(browseGt500Lte1000,ScoreFunctionBuilders.weightFactorFunction(640)));
        filterFunctionBuilders.add(new FilterFunctionBuilder(browseGt100Lte500,ScoreFunctionBuilders.weightFactorFunction(400)));
        filterFunctionBuilders.add(new FilterFunctionBuilder(browseLt100,ScoreFunctionBuilders.weightFactorFunction(80)));

        FilterFunctionBuilder[] builders = new FilterFunctionBuilder[filterFunctionBuilders.size()];
        filterFunctionBuilders.toArray(builders);
        // 权重
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
                .boostMode(CombineFunction.SUM).scoreMode(FunctionScoreQuery.ScoreMode.SUM);

        BoolQueryBuilder must = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("name", "权益")).
                must(functionScoreQueryBuilder);
        builder.query(must);
        request.source(builder);

        //3.执行查询
        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
        //4.获取到_source中的数据
        for (SearchHit hit : resp.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap.toString()+hit.getScore());
        }
    };

    public static void main(String[] args) {
        ConnectElasticsearch.connect(SEARCH_WITH_ORDER);

    }
//    @Test
//    public void weightFunctionQuery() throws IOException {
//        //1.创建request
//        SearchRequest request = new SearchRequest("goods");
//        //2.指定查询条件
//
//        SearchSourceBuilder builder = new SearchSourceBuilder();
//        BoolQueryBuilder newNumberLte30 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("newNumber").lte(30));
//        BoolQueryBuilder newNumberGt30Lte60 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("newNumber").gt(30).lte(60));
//        BoolQueryBuilder newNumberGt60Lte90 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("newNumber").gt(60).lte(90));
//        BoolQueryBuilder newNumberGt90 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("newNumber").gt(90));
//
//        BoolQueryBuilder browseGt1000 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("browse").gt(1000));
//        BoolQueryBuilder browseGt500Lte1000 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("browse").gt(500).lte(100));
//        BoolQueryBuilder browseGt100Lte500 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("browse").gt(100).lte(500));
//        BoolQueryBuilder browseLt100 = QueryBuilders.boolQuery().filter(QueryBuilders.rangeQuery("browse").lt(100));
//
//        List<FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
//
//        filterFunctionBuilders.add(new FilterFunctionBuilder(newNumberLte30,ScoreFunctionBuilders.weightFactorFunction(1000)));
//        filterFunctionBuilders.add(new FilterFunctionBuilder(newNumberGt30Lte60,ScoreFunctionBuilders.weightFactorFunction(900)));
//        filterFunctionBuilders.add(new FilterFunctionBuilder(newNumberGt60Lte90,ScoreFunctionBuilders.weightFactorFunction(500)));
//        filterFunctionBuilders.add(new FilterFunctionBuilder(newNumberGt90,ScoreFunctionBuilders.weightFactorFunction(100)));
//        filterFunctionBuilders.add(new FilterFunctionBuilder(browseGt1000,ScoreFunctionBuilders.weightFactorFunction(800)));
//        filterFunctionBuilders.add(new FilterFunctionBuilder(browseGt500Lte1000,ScoreFunctionBuilders.weightFactorFunction(640)));
//        filterFunctionBuilders.add(new FilterFunctionBuilder(browseGt100Lte500,ScoreFunctionBuilders.weightFactorFunction(400)));
//        filterFunctionBuilders.add(new FilterFunctionBuilder(browseLt100,ScoreFunctionBuilders.weightFactorFunction(80)));
//
//        FilterFunctionBuilder[] builders = new FilterFunctionBuilder[filterFunctionBuilders.size()];
//        filterFunctionBuilders.toArray(builders);
//        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders).boostMode(CombineFunction.SUM).scoreMode(FunctionScoreQuery.ScoreMode.SUM);
//        BoolQueryBuilder must = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("name", "权益")).must(functionScoreQueryBuilder);
//        builder.query(must);
//        request.source(builder);
//        //3.执行查询
//        SearchResponse resp = client.search(request, RequestOptions.DEFAULT);
//        //4.获取到_source中的数据
//        for (SearchHit hit : resp.getHits().getHits()) {
//            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
//            System.out.println(sourceAsMap.toString()+hit.getScore());
//        }
//    }
}
